//
//  TestTouchAndRendering.cpp
//  Nano
//
//  Created by Jeppe Nielsen on 8/24/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "GameState.hpp"
#include "GameWorld.hpp"
#include "RenderSystem.hpp"
#include "Touchable.hpp"
#include "Material.hpp"
#include "TextureComponent.hpp"
#include "TouchSystem.hpp"
#include "FirstPersonMoverSystem.hpp"

using namespace Nano;

class TestTouchAndRendering : public GameState {
    void Initialized() {
        
        Input.TouchUp += event_handler(this, &TestTouchAndRendering::TouchUp);
        
        renderer = world.CreateSystem<RenderSystem>();
        
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
        world.CreateSystem<TouchSystem>()->Input = &Input;
        
        GameObject* camera = world.CreateObject();
        camera->AddComponent<Camera>()->Viewport = Viewport();
        camera->AddComponent<Transform>()->Position = Vector3(0,0,4);
        camera->AddComponent<FirstPersonMover>();
        
        cubeAsset = world.CreateObject();
        cubeAsset->AddComponent<Mesh>()->AddCube(0, 1, Colour::White());
        cubeAsset->AddComponent<Material>()->BlendMode = BlendMode::None;
        cubeAsset->AddComponent<TextureComponent>()->Texture().LoadFromPng("grass.png");
        
        GameObject* cubeAsset2 = world.CreateObject();
        cubeAsset2->AddComponent<Mesh>()->AddGeoSphere(0, 1, 10);
        
        /*
         cubeInstance = world.CreateObject();
         cubeInstance->AddComponent<Transform>();
         cubeInstance->AddComponent<Mesh>(cubeAsset2);
         cubeInstance->AddComponent<Material>(cubeAsset);
         cubeInstance->AddComponent<TextureComponent>(cubeAsset);
         cubeInstance->AddComponent<Touchable>()->Click += event_handler(this, &SpinningCube::SphereClicked);
         */
        
        
        
        for (int x = -10; x<=10; x++) {
            for (int y=0; y<10; y++) {
                cubeInstance = world.CreateObject();
                cubeInstance->AddComponent<Transform>();
                cubeInstance->AddComponent<Mesh>(y & 1 && x & 1 ? cubeAsset2 : cubeAsset);
                cubeInstance->AddComponent<Material>(cubeAsset);
                cubeInstance->AddComponent<TextureComponent>(cubeAsset);
                cubeInstance->GetComponent<Transform>()->Position = Vector3(x*3,3 * y,0);
                cubeInstance->AddComponent<Touchable>()->Click += event_handler(this, &TestTouchAndRendering::SphereClicked);
            }
        }
        
        
    }
    
    void SphereClicked(TouchData touch) {
        touch.Touchable->Owner()->GetComponent<Transform>()->Position += touch.WorldNormal * 0.5f;
    }
    
    void Update(float dt) {
        static float rotation = 0;
        rotation+=dt;
        cubeInstance->GetComponent<Transform>()->Rotation = Quaternion::Create(rotation, Vector3(1,1,0).Normalized());
        world.Update(dt);
    }
    
    void Render() {
        glClearColor( 0.0f, 0.2f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_CULL_FACE);
        renderer->Render();
    }
    void TouchUp(TouchEvent e) {
        //Exit();
    }
    
    GameWorld world;
    RenderSystem* renderer;
    GameObject* cubeAsset;
    GameObject* cubeInstance;
};

/*
int main() {
    Engine e;
    e.Start<TestTouchAndRendering>();
}
 */
